/*
 * @Author: 无聊 7989314+Hwz1997@user.noreply.gitee.com
 * @Date: 2025-09-03 02:11:00
 * @LastEditors: 无聊 7989314+Hwz1997@user.noreply.gitee.com
 * @LastEditTime: 2025-09-29 09:13:00
 * @FilePath: \keep_motion_api\controllers\community\posts.controller.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

const { CommunityPost } = require('@/models/community');

const getPosts = async(req, res) => {
  try {
    const {user_name, user_id, topic, activity_id, related_activity, course_id, related_course, pageNum, pageSize } = req.query;
    const offset = (pageNum - 1) * pageSize;
    const limit = pageSize;
    const posts = await CommunityPost.getPosts(user_name, user_id, topic, activity_id, related_activity, course_id, related_course, offset, limit);
    res.status(200).send(posts);
  } catch (error) {
    res.status(500).json({ message: '查询帖子失败', error });
  }
}
const deletePost = async(req, res) => {
  try {
    const { id } = req.params;
    const post = await CommunityPost.deletePost(id);
    res.status(200).send(post);
  } catch (error) {
    res.status(500).json({ message: '删除帖子失败', error });
  }
}
const pinPost = async(req, res) => {
  try {
    const { id, topic } = req.body;
    const post = await CommunityPost.pinPost(id, topic);
    res.status(200).send(post);
  } catch (error) {
    res.status(500).json({ message: '置顶帖子失败', error });
  }
}
const deleteComment = async(req, res) => {
  try {
    const { id, comment_id } = req.body;
    const post = await CommunityPost.deleteComment(id, comment_id);
    res.status(200).send(post);
  } catch (error) {
    res.status(500).json({ message: '删除评论失败', error });
  }
}

module.exports = {
  getPosts,
  deletePost,
  pinPost,
  deleteComment
}